home *** CD-ROM | disk | FTP | other *** search
/ El Mac 9 / El Mac 9.iso / Shareware / Applications / MathPad 2.4 / Examples / curve fit < prev    next >
Encoding:
Text File  |  1994-05-22  |  516 b   |  25 lines  |  [TEXT/MPad]

  1. ----------general least squares fit------
  2. -- finds coefficients a[i] for best fit of weighted sum of functions of x
  3. include ":incl:matrix ops"
  4.  
  5. fit(x) = sum(a[i,1]*f(x)[i],i,1,nparms)
  6.  
  7. f(x)[j]=x^(j-1)  -- polynomial a1+a2*x+a3*x^2...
  8.  
  9. P[i,j]=f(x[i])[j] dim[ndata,nparms]
  10. PtP:=multiply(transpose(P),P):
  11. Pty:=multiply(transpose(P),y):
  12. inv:=invert(PtP):
  13. a:=multiply(inv,Pty):
  14.  
  15. data=read(xydata)
  16. x[i]=data[i,1]; y[i]=data[i,2] dim[ndata]; 
  17.  
  18. ndata=count(data)
  19. nparms=3
  20.  
  21. a:{{5.582},{0.159},{0.006}}
  22.  
  23. plot data
  24. plot fit(X)
  25.